<?php


namespace app\service;


use think\Db;

class cxyunPayService
{
    public function getCarOrderMoney($info){
        $data["parkingId"]=$info["parkingId"];
        $data["plateNumber"]=$info["plateNumber"];
        $data["timestamp"]=time()."000";
        $notData=array();
        $cxyunApi=new cxyunService();
        $res=$cxyunApi->commonPost("calculatingTempCost",$data,$notData);

        $log["plateNumber"]=$data["plateNumber"];
        $log["parkingNo"]=$data["parkingId"];
        $log["cartypeName"]=$info["cartypeName"];
        $log["carmodelName"]=$info["carmodelName"];
        $log["amount"]=$info["amount"];
        $log["result"]=$res;
        $log["createTime"]=date('Y-m-d H:i:s');
        Db::table('cxyun_calculatingTempCost')->insert($log);

        $result= json_decode($res,true);
        return $result;
    }
    //查询是否是月租车或福利车
    public function getCarInfo($info){
        return Db::table("cxyun_car")
            ->where("parkingId",$info["parkingId"])
            ->where("plateNumber",$info["plateNumber"])
            ->find();
    }
    public function getWelfareCarInfo($info){
        return Db::table("cxyun_car")
            ->where("parkingId",$info["parkingId"])
            ->where("plateNumber",$info["plateNumber"])
            ->where("carType",2)
            ->find();
    }

    public function getPayInfo($info){
        return Db::table("cxyun_pay_bill")
            ->where("parkingId",$info["parkingId"])
            ->where("recordId",$info["recordId"])
            ->find();
    }

    //月租车扣费
    public function monthlyCarPay($info,$roominfo){
        $money=$roominfo["monthly_car_balance"]-$info["amount"];
        //月租车 扣除房屋月租车金额

        //账单流水插入一条记录
        //更新房屋账单金额
        Db::startTrans();
        try {
            $id=Db::table("cxyun_pay_bill")->insertGetId($info);
            //更新房屋余额
            Db::table("roominfo")
                ->where("id",$roominfo["id"])
                ->update(["monthly_car_balance"=>$money]);;
            // 提交事务
            Db::commit();
            return array("code"=>0,"msg"=>"","billid"=>$id);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return array("code"=>500,"msg"=>$e->getMessage());
        }
    }

    //福利车扣费
    public function welfareCarPay($info,$roominfo){
        $money=$roominfo["welfare_car_balance"]-$info["amount"];
        //月租车 扣除房屋月租车金额
        //账单流水插入一条记录
        //更新房屋账单金额
        Db::startTrans();
        try {
            $info["createTime"]=date("Y-m-d H:i:s");
            $id=Db::table("cxyun_pay_bill")->insertGetId($info);
            //更新房屋余额
            Db::table("roominfo")
                ->where("id",$roominfo["id"])
                ->update(["welfare_car_balance"=>$money]);
            // 提交事务
            Db::commit();
            return array("code"=>0,"msg"=>"","billid"=>$id);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return array("code"=>500,"msg"=>$e->getMessage());
        }
    }
}